In Java, classes and objects form the foundation of object-oriented programming (OOP). A class is a blueprint that describes how objects of that type behave and what data they store. An object is a real instance created from that blueprint. You can create many different objects from the same class, and each object stores its own unique data.
A class defines two major parts:
These are the pieces of data every object of the class will store.
private String name;
private int age;
These are the actions the object can perform.
public void setAge(int a) { age = a; }
public int getAge() { return age; }
A class itself is NOT an object. It is only the design.
An object is created from a class using the new keyword.
Dog d1 = new Dog();
Dog d2 = new Dog();
Each of these statements creates a new Dog object in memory.
The variable (e.g., d1) is a reference variable that points to the object.
Even though objects come from the same class, they can hold different data.
Student s1 = new Student("Aiden", 95);
Student s2 = new Student("Jolie", 87);
s1 stores Aiden's data, while s2 stores Jolie's data. This is why objects represent individual items with their own state.
You access an object's behavior using the dot (.) operator:
s1.getGrade();
s2.setGrade(100);
Object variables do not store the object directly—they store a reference (memory address) pointing to the object.
Student s1 = new Student("Ben", 90);
Here:
Class: Car Blueprint
Objects: Actual Cars (Toyota, Honda, Tesla)
Each car shares the same design but has different colors, mileage, and owners.
This is exactly how class instances differ in Java.
| Term | Definition |
|---|---|
| Class | Blueprint defining attributes (state) and methods (behavior) |
| Object | A real instance created from a class |
| Instance Variable | Data stored inside each object |
| Method | Behavior an object can perform |
| Instantiation | Creating an object using new |
| State | The values stored in an object |
| Behavior | Actions the object performs |
| Reference Variable | Stores the memory address of an object |
public class Dog {
private String name;
private int age;
public Dog(String n, int a) {
name = n;
age = a;
}
public void bark() {
System.out.println(name + " says Woof!");
}
}
Dog d1 = new Dog("Max", 4);
Dog d2 = new Dog("Bella", 2);
d1.bark(); // Max says Woof!
d2.bark(); // Bella says Woof!
Understanding objects helps students succeed in:
this keyword